<?
require_once("header.misc.php");

function getIndicators($hid) {
	connectToDB();
	$rawhabit=mysql_query("SELECT * FROM `habit_indicator` WHERE `habit_id`='" . mysql_real_escape_string($hid) . "'");
	$return=array();
	while($habit=mysql_fetch_array($rawhabit)) {
		$return[$habit['habit_indicator_id']]=$habit;
	}
	return $return;
}

function getHabitList() {
	connectToDB();
	$rawhabit=mysql_query("SELECT * FROM `habit`");
	$return=array();
	while($habit=mysql_fetch_array($rawhabit)) {
		$return[$habit['habit_id']]=$habit;
	}
	return $return;
}

function getHabitById($hid) {
	connectToDB();
	$habit=mysql_query("SELECT * FROM `habit` WHERE `habit_id`='" . mysql_real_escape_string($hid) . "'");
	return mysql_fetch_array($habit);
}

function getIndicatorById($hid) {
	connectToDB();
	$habit=mysql_query("SELECT * FROM `habit_indicator` WHERE `habit_indicator_id`='" . mysql_real_escape_string($hid) . "'");
	return mysql_fetch_array($habit);
}

function getDescById($hid) {
	connectToDB();
	$habit=mysql_query("SELECT * FROM `habit_description` WHERE `habit_description_id`='" . mysql_real_escape_string($hid) . "'");
	return mysql_fetch_array($habit);
}

function getLevelList() {
	connectToDB();
	$rawlist=mysql_query("SELECT * FROM `level` ORDER BY `level_level` ASC");
	$return=array();
	while($list=mysql_fetch_array($rawlist)) {
		$return[$list['level_level']]=$list;
	}
	return $return;
}

function getDescByIndLev($ind,$lev) {
	connectToDB();
	$rawind=mysql_query("SELECT * FROM `habit_description` WHERE `habit_indicator_id`='" . mysql_real_escape_string($ind) . "' AND `level_id`='" . mysql_real_escape_string($lev) . "'");
	if(!$rawind) {
		return false;
	}
	if(mysql_num_rows($rawind)<1) {
		return false;
	}
	return mysql_fetch_array($rawind);
}

function deleteIndicator($id) {
	connectToDB();
	mysql_query("DELETE FROM `habit_indicator` WHERE `habit_indicator_id`='" . mysql_real_escape_string($id) . "'");
	mysql_query("DELETE FROM `habit_description` WHERE `habit_indicator_id`='" . mysql_real_escape_string($id) . "'");
}

function deleteHabit($id) {
	connectToDB();
	$inds=getIndicators($id);
	foreach($inds as $ind) {
		deleteIndicator($ind['habit_indicator_id']);
	}
	mysql_query("DELETE FROM `habit` WHERE `habit_id`='" . mysql_real_escape_string($id) . "'");
}

function newDescription($ind,$lev) {
	connectToDB();
	mysql_query("INSERT INTO `habit_description` (`habit_indicator_id`,`level_id`) VALUES ('" . mysql_real_escape_string($ind) . "','" . mysql_real_escape_string($lev) . "')");
	return mysql_insert_id();
}

function newHabit() {
	connectToDB();
	mysql_query("INSERT INTO `habit` (`habit_name`) VALUES ('New Habit')");
	return mysql_insert_id();
}

function newIndicator($habit) {
	connectToDB();
	mysql_query("INSERT INTO `habit_indicator` (`habit_id`,`habit_indicator_name`) VALUES ('" . mysql_real_escape_string($habit) . "','New Indicator')");
	return mysql_insert_id();
}

function changeHabitName($id,$newname) {
	connectToDB();
	mysql_query("UPDATE `habit` SET `habit_name`='" . mysql_real_escape_string($newname) . "' WHERE `habit_id`='" . mysql_real_escape_string($id) . "'");
}

function changeIndicatorName($id,$newname) {
	connectToDB();
	mysql_query("UPDATE `habit_indicator` SET `habit_indicator_name`='" . mysql_real_escape_string($newname) . "' WHERE `habit_indicator_id`='" . mysql_real_escape_string($id) . "'");
}

function changeDescription($id,$newname) {
	connectToDB();
	mysql_query("UPDATE `habit_description` SET `habit_description_description`='" . mysql_real_escape_string($newname) . "' WHERE `habit_description_id`='" . mysql_real_escape_string($id) . "'");
}

